package cn.zhp.controller;

import cn.zhp.entity.Result;
import cn.zhp.entity.SysRole;
import cn.zhp.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manager/role")
public class SysRoleController {

    @Autowired
    SysRoleService service;
   
    @RequestMapping()
    public ModelAndView index(){
        return new ModelAndView("/role/role");
    }


    @RequestMapping("list")
    public Result list(){
        return new Result(true,"操作成功",service.selectAll());
    }

    @RequestMapping("selectPage/{pageNum}/{pageSize}")
    public Result listPage(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize, @RequestBody Map<String,Object> params){
        return new Result(true,"操作成功",service.selectPage(pageNum,pageSize,params));

    }

    @RequestMapping("toUpdate")
    public ModelAndView toUpdate(){
        return new ModelAndView("/role/role-save");
    }

    @RequestMapping("toDetail")
    public ModelAndView toDetailPage(){
        return new ModelAndView("/role/role-detail");
    }

    @RequestMapping("toRoleUser")
    public ModelAndView toRoleUser(){
        return new ModelAndView("/role/role-user");
    }


    //批量删除角色的已选用户
    //客户端直接传递{ids:this.yxIds}  ->实际会被浏览器自动序列化成字符串放入url中 序列化的时候会把数组的参数名修改调
    //ids[]:xxx  需要自行处理序列化  组装
    @RequestMapping("deleteBatch")
    public Result deleteBatch(long rid,long[] ids){
        return new Result(true,"操作成功",service.deleteBatch(rid, ids));
    }

    //如果通过list自动封装   会以List<Integer>进行封装
    @RequestMapping("insertBatch")
    public Result insertBatch(long rid,Long[] cids){
        //将数组转换成list
        List<Long> list = Arrays.asList(cids);
        return new Result(true,"操作成功",service.insertBatch(rid,list));
    }


    @RequestMapping("doUpdate")
    public Result doUpdate(@RequestBody SysRole sysRole){
        Result result = new Result();
        service.updateByPrimaryKeySelective(sysRole);
        return new Result(true,"更新成功",null);
    }



}
